﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RabbitMQ_Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RabbitMQ_COM.Exchange
{
    public class WorkerService
    {
        public static void Consumption()
        {
            var rounteKey = "test";
           
            using (var connection = RabbitMQHelper.GetConnection())
            {
                using (var channel = connection.CreateModel())//创建信道，通信管道
                {
                    //创建队列
                    channel.QueueDeclare(rounteKey, false, false, false, null);
                    while (true)
                    {
                        //没绑定交换机，但由于rabbitmq有默认交换机
                        var consumer = new EventingBasicConsumer(channel);
                        channel.BasicQos(0, 1, false);//设置消费者权重
                        consumer.Received += (model, ea) =>
                        {
                            var message = "";
                            message = Encoding.UTF8.GetString(ea.Body.ToArray());
                            Console.WriteLine(message);

                        };
                        channel.BasicConsume(rounteKey, true, consumer);
                        Thread.Sleep(1000);
                    }


                }
            }
        }
    }
}
